package com.ce.pms.houseMainLine;

import java.math.BigDecimal;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.service.ICashieringService;
import com.ce.pms.charge.vo.CashieringSearchVO;
import com.ce.pms.charge.vo.HistoryAccountVO;
import com.ce.pms.charge.vo.HouseChargeVO;
import com.ce.pms.customer.service.ICustomerService;
import com.ce.pms.customer.vo.CustSearchVO;
import com.ce.pms.custservice.service.IEventManageService;
import com.ce.pms.custservice.vo.CustServiceEventSearchVO;
import com.ce.pms.house.service.IProRightChangeService;
import com.ce.pms.occupation.vo.OccupationQueryConditionVO;
import com.ce.pms.occupation.vo.PerfectItemSearchVO;
import com.ce.pms.renovation.service.IInspectionService;
import com.ce.pms.renovation.service.IRenovateChargeService;
import com.ce.pms.renovation.service.IRenovateManageService;
import com.ce.pms.renovation.service.IRenovateSupervisionService;
import com.ce.pms.renovation.vo.DepositAccountingListVO;
import com.ce.pms.renovation.vo.DepositAccountingQueryVO;
import com.ce.pms.renovation.vo.InspectionRequisitionQueryConditionVO;
import com.ce.pms.renovation.vo.RenovateRequisitionQueryVO;
import com.ce.pms.renovation.vo.RenovateRequisitionVO;
import com.ce.pms.renovation.vo.RenovateViolationConditionVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

public class HouseMainLineAction extends PmsBaseAction{

	/**
	 * 功能描述:初始化房间主线主页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2009-1-12<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String houseMainLineMain(){
		return "houseMainLineMain";
	}

	/**
	 * 功能描述:根据输入的房间号码进行查询
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2009-1-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String getResearchList(@Read(key="houseNumber",defaultValue="")String houseNumber,
			@Read(key = "ec_crd",defaultValue="10")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){

		//保存到ruquest中去，在分页中应用
		this.getRequest().setAttribute("houseNumber", houseNumber);

		//设置查询条件
		CashieringSearchVO searchVO = new CashieringSearchVO();
		searchVO.setHouseNumber(houseNumber);
		searchVO.setPageSize(pageSize);
		searchVO.setPageNo(pageNo);
		Page page=new Page();

		if(!(BlankUtil.isBlank(houseNumber))){
             //获得当前有效的房间列表
			page = cashieringService.getEffectiveHouseChargeInfo(searchVO);

			//当前有效房间列表没有记录时，，才查询历史中房间的列表
			if(page.getResult().size()<1){
				page=cashieringService.getLastHouseChargeInfo(searchVO);
			}
		}

		this.getRequest().setAttribute("cashierList", page.getResult());
		this.getRequest().setAttribute("totalRows", new Integer(page.getResult().size()));

        return "houseMainLineList";

	}

	/**
	 * 功能描述:获得房间的详细（包括银行帐户信息，欠费信息，余额信息）
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-8-29
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String getHouseChargeMessage(@Read(key="keyword")Long keyword,
			@Read(key="link")@Out(key="link") boolean link,
			@Read(key="proRightId")Long proRightId,
			@Read(key="accountId")Long accountId,
			@Read(key="chargeSelect")String chargeSelect){

		CashieringSearchVO searchVo = new CashieringSearchVO();
		searchVo.setHouseId(keyword);
		searchVo.setProRightId(proRightId);
		searchVo.setAccountId(accountId);
		HistoryAccountVO vo=cashieringService.queryHouseChargeDetailInfo(searchVo);
        if(BlankUtil.isBlank(chargeSelect)){
        	chargeSelect=vo.getChargeSelect();
        }
		String forward="";

		//如果房间是产权房或者管理房（帐户是合并记账）
		if(BlankUtil.isBlank(vo.getRelHouses())){

			forward="houseMainLineMessage";

			//如果房间是在某个管理房，但又是分开记账的
		} else {
			forward="houseMainLineRelationMessage";
		}

       //isChangeProRight 房间是否已经发生过产权变更记录： 1是  0否
		if(!BlankUtil.isBlank(vo.getIsChangeProRight()) && HouseChargeVO.IS_CHANGE_PRO_RIGHT_1.equals(vo.getIsChangeProRight())){

			this.getRequest().setAttribute("showButtomType", "1");

		}


		this.getRequest().setAttribute("view", vo);

		return forward;
	}

	/**
	 * 功能描述:住户详细资料<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2009-1-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String queryCustomerInfo(@Read	CustSearchVO custsearchVO,
			@Read(key = "ec_p",defaultValue="1")	Integer pageNO,
			@Read(key = "ec_crd",defaultValue="15") Integer pageSize){
		String sortStr = ExtremeUtils.getOrderString(this.getRequest());
		custsearchVO.setOrderString(sortStr);
		Page page=this.customerService.queryCustomerInfo(custsearchVO, pageNO, pageSize);

		this.setAttribute("customerList", page.getResult());
		this.setAttribute("totalRows", ((Long)page.getTotalCount()).intValue());
		this.getRequest().setAttribute("HouseIds", custsearchVO.getHouseIds());
		return "customerList";
	}

	/**
	 * 功能描述:房屋详细资料的tab主页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2009-1-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String houseDetailTabMain(@Read(key="houseId")@Out(key="houseId")Long houseId){

		return "houseDetailTabMain";
	}

	/**
	 * 功能描述:入伙管理<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2009-1-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String occupationMain(@Read(key="houseId")@Out(key="houseId")Long houseId){
		OccupationQueryConditionVO queryConditionVO=new OccupationQueryConditionVO();
		queryConditionVO.setHouseId(houseId);
		setAttribute("condition", queryConditionVO, Scope.SESSION);
		PerfectItemSearchVO perfectItemSearchVO =new PerfectItemSearchVO();
		perfectItemSearchVO.setHouseId(houseId);
		this.setAttribute("perfectItemSearchVO", perfectItemSearchVO,
				Scope.SESSION);
		return "occupationMain";
	}

	/**
	 * 功能描述:费用查询<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2009-1-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String houseChargeMain(@Read(key="houseId")@Out(key="houseId")Long houseId,
			@Read(key="accountId")@Out(key="accountId")Long accountId){
		return "houseChargeMain";
	}

	/**
	 * 功能描述:  <br>
	 * @param houseNumber
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-19  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initCustomerEventMain(@Read(key="houseNumber")@Out(key="houseNumber")String houseNumber){
		return "customerEventMain";
	}
	/**
	 * 功能描述:查询客服事件<br>
	 * @param custServiceEventSearchVO
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-19  <br>
	 */
	public String listCustomerEvent(@Read CustServiceEventSearchVO custServiceEventSearchVO,
									@Read(key = "ec_crd",defaultValue="10")int pageSize,
									@Read(key = "ec_p", defaultValue="1")	int pageNo){
		custServiceEventSearchVO.setPageNo(pageNo);
		custServiceEventSearchVO.setPageSize(pageSize);
		custServiceEventSearchVO.setSortStr(ExtremeUtils.getOrderString(getRequest()));

		Page page = eventManageService.queryCustServEventByPage(custServiceEventSearchVO);
		setAttribute("totalRows", Integer.valueOf(String.valueOf(page.getTotalCount())));
		setAttribute("customerEventList",page.getResult());
		//缓存房号
		setAttribute("houseNumber", custServiceEventSearchVO.getHouseNumber());
		return "customerEventList";
	}
	/**
	 * 功能描述:产权变更查询<br>
	 * @param houseNumber
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public String listProRightChange(@Read(key="houseNumber")@Out(key="houseNumber")String houseNumber,
									 @Read(key = "ec_crd",defaultValue="10")int pageSize,
									 @Read(key = "ec_p", defaultValue="1")	int pageNo){
		Page page = proRightChangeService.queryHisGoOutDoListByPage(Long.valueOf(0), houseNumber, null, super.getUserId(), pageNo, pageSize);
		setAttribute("totalRows",Integer.valueOf(String.valueOf(page.getTotalCount())));
		setAttribute("proRightList", page.getResult());
		return "proRightChangeList";
	}
	/**
	 * 功能描述: 跳转到装修管理主页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-20  <br>
	 */
	public String initRenovationMain(@Read(key="houseNumber")@Out(key="houseNumber")String houseNumber){
		return "renovationMain";
	}
	/**
	 * 功能描述:装修申请单查询 <br>
	 * @param queryVO
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-20  <br>
	 */
	public String queryRequisitions(@Read RenovateRequisitionQueryVO queryVO,
									@Read(key = "ec_crd",defaultValue="10")	int pageSize,
									@Read(key = "ec_p", defaultValue="1")	int pageNo){

		queryVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));
		queryVO.setPageNo(pageNo);
		queryVO.setPageSize(pageSize);
		Page page = renovateManageService.queryRequisitions(queryVO);
		Long totalRows = page.getTotalCount();
		List<RenovateRequisitionVO> reqlist = page.getResult();

		setAttribute("totalRows",totalRows.intValue());
		setAttribute("reqlist", reqlist);
		setAttribute("houseNumber", queryVO.getHouseNumber());
		return "renovateRequisitionList";
	}
	/**
	 * 功能描述:违规登记查询 <br>
	 * @param queryCondition
	 * @param pageNO
	 * @param pageSize
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-20  <br>
	 */
	public String queryRenovateViolationInfo(
			@Read RenovateViolationConditionVO queryCondition,
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd",defaultValue = "10")Integer pageSize){
		queryCondition.setPageNo(pageNO);
		queryCondition.setPageSize(pageSize);
		Page page = renovateSupervisionService.queryRenovateViolations(queryCondition);
		setAttribute("renovateViolationList", page.getResult());
		setAttribute("totalRows",Integer.valueOf(String.valueOf(page.getTotalCount())));

		setAttribute("houseNumber", queryCondition.getHouseNumber());
		return "renovateViolationList";
	}
	/**
	 * 查询验收结果
	 *
	 * @param inspectionRequisitionQueryConditionVO
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public String queryInspectionRequisition(
			@Read InspectionRequisitionQueryConditionVO inspectionRequisitionQueryConditionVO,
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd", defaultValue = "10") Integer pageSize) {

			inspectionRequisitionQueryConditionVO.setPageNo(pageNO);
			inspectionRequisitionQueryConditionVO.setPageSize(pageSize);
			inspectionRequisitionQueryConditionVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));

			Page page = inspectionService.queryInspectionRequisitions(inspectionRequisitionQueryConditionVO);
			this.setAttribute("resultDataList", page.getResult());
			this.setAttribute("totalRows", Integer.valueOf(String.valueOf(page.getTotalCount())));
			//缓存房号
			setAttribute("houseNumber", inspectionRequisitionQueryConditionVO.getHouseNumber());

			return "inspectionRequistionList";
	}
	/**
	 * 功能描述:查询列表  <br>
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String queryDepositAccounting(@Read DepositAccountingQueryVO queryVO,
			@Read(key = "ec_crd",defaultValue="10")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){

			queryVO.setPageNo(pageNo);
			queryVO.setPageSize(pageSize);
			queryVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));

			Page page = renovateChargeService.queryDepositAccountings(queryVO);
			Long totalRows = page.getTotalCount();
			List<DepositAccountingListVO> depositList = page.getResult();
			BigDecimal countSum = new BigDecimal(0);
			for (DepositAccountingListVO listVO : depositList) {
				if(!BlankUtil.isBlank(listVO.getReturnSum())){
					BigDecimal temp = listVO.getReturnSum();
					countSum = countSum.add(temp);
				}
			}

			setAttribute("countSum", countSum);
			setAttribute("depositList", depositList);
			setAttribute("totalRows",totalRows.intValue());
			setAttribute("houseNumber", queryVO.getHouseNumber());

		return "depositAccountingList";
	}
	/**
	 * 保证金退还
	 */
	private  IRenovateChargeService renovateChargeService;
	/**
	 * 装修验收Service
	 */
	private IInspectionService	inspectionService;
	//装修监管Service
	private IRenovateSupervisionService renovateSupervisionService;
	//装修申请SERVICE
	private IRenovateManageService renovateManageService;
	// 产权变更Service
	private IProRightChangeService proRightChangeService;

	private ICashieringService cashieringService;

	protected ICustomerService customerService;

	private IEventManageService eventManageService; //客户事件Service

	public void setCashieringService(ICashieringService cashieringService) {
		this.cashieringService = cashieringService;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public void setEventManageService(IEventManageService eventManageService) {
		this.eventManageService = eventManageService;
	}

	public void setProRightChangeService(
			IProRightChangeService proRightChangeService) {
		this.proRightChangeService = proRightChangeService;
	}

	public void setRenovateManageService(
			IRenovateManageService renovateManageService) {
		this.renovateManageService = renovateManageService;
	}

	public void setRenovateSupervisionService(
			IRenovateSupervisionService renovateSupervisionService) {
		this.renovateSupervisionService = renovateSupervisionService;
	}

	public void setInspectionService(IInspectionService inspectionService) {
		this.inspectionService = inspectionService;
	}

	public void setRenovateChargeService(
			IRenovateChargeService renovateChargeService) {
		this.renovateChargeService = renovateChargeService;
	}

}
